﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IntfClass;

namespace IntfSetting.FrmIntf
{
    public partial class FrmFreStep2 : Form
    {
        public FrmFreStep2(string ParentID, string Frequency)
        {
            InitializeComponent();
            if (!ParentID.Equals(string.Empty))
            {
                //blnNew = false;
                strParentID = ParentID;
                strFre = Frequency;
                txtFre.Text = Frequency;
                
            }
        }

        private string strParentID = string.Empty;
        //private Boolean blnNew = true;
        public DataTable dtGridView = new DataTable();
        public DataRow drSource = null;
        public string strFre = string.Empty;
        //public Boolean blnTiming = true;

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmFreStep3 frmFreStep3 = new FrmFreStep3(string.Empty,string.Empty,1);
            if (frmFreStep3.ShowDialog() == DialogResult.OK)
            {
                DataRow drRow = dtGridView.NewRow();
                drRow["StartTime"] = frmFreStep3.strTime;
                drRow["EndTime"] = frmFreStep3.endTime;

                dtGridView.Rows.Add(drRow);

                this.dataGridView1.DataSource = dtGridView;
                dataGridView1.Columns[0].Visible = false;
                dataGridView1.Columns[1].Visible = false;
                dataGridView1.Columns[2].Visible = true;
                dataGridView1.Columns[3].Visible = true;

                dataGridView1.Columns[2].ReadOnly = true;
                dataGridView1.Columns[3].ReadOnly = true;

                dataGridView1.Columns[2].HeaderText = "开始时间";
                dataGridView1.Columns[3].HeaderText = "结束时间";

            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.dtGridView.Clear();
            this.Dispose();
        }

        //private void radioButton1_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (radioButton1.Checked)
        //    {
        //        this.txtFre.ReadOnly = false;
        //        this.panel2.Enabled = false;
        //        blnTiming = true;
        //    }
        //    else
        //    {
        //        this.txtFre.ReadOnly = true;
        //        this.panel2.Enabled = true;
        //        blnTiming = false;
        //    }
        //}

        private void FrmFreStep2_Load(object sender, EventArgs e)
        {
            SearchStartTime();
            this.dataGridView1.DataSource = dtGridView;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].Visible = false;
            dataGridView1.Columns[2].Visible = true;
            dataGridView1.Columns[3].Visible = true;

            dataGridView1.Columns[2].ReadOnly = true;
            dataGridView1.Columns[3].ReadOnly = true;

            dataGridView1.Columns[2].HeaderText = "开始时间";
            dataGridView1.Columns[3].HeaderText = "结束时间";

        }

        private void SearchStartTime()
        {
            StringBuilder strSql = new StringBuilder();
            if (strParentID.Equals(string.Empty))
            {
                strSql.AppendLine("select * from TRuleList t where t.ParentID = 0");
            }
            else
            {
                strSql.AppendLine("select * from TRuleList t where t.ParentID = " + strParentID);
            }
            ClassDB classDB = new ClassDB();
            dtGridView = classDB.ExecuteDataTable(strSql.ToString());

        }

        private void btnUpt_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows[0].Cells["StartTime"].Value == null)
            {
                return;
            }
            FrmFreStep3 frmFreStep3 = new FrmFreStep3(dataGridView1.SelectedRows[0].Cells["StartTime"].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells["EndTime"].Value.ToString(),1);
            if (frmFreStep3.ShowDialog() == DialogResult.OK)
            {
                dataGridView1.SelectedRows[0].Cells["StartTime"].Value = frmFreStep3.strTime;
                dataGridView1.SelectedRows[0].Cells["EndTime"].Value = frmFreStep3.endTime;

                this.dataGridView1.DataSource = dtGridView;
                dataGridView1.Columns[0].Visible = false;
                dataGridView1.Columns[1].Visible = false;
                dataGridView1.Columns[2].Visible = true;
                dataGridView1.Columns[3].Visible = true;

                dataGridView1.Columns[2].ReadOnly = true;
                dataGridView1.Columns[3].ReadOnly = true;

                dataGridView1.Columns[2].HeaderText = "开始时间";
                dataGridView1.Columns[3].HeaderText = "结束时间";


            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            if (txtFre.Text.Equals(string.Empty))
            {
                MessageBox.Show("频率值不可为空！");
            }
            strFre = txtFre.Text;

            //if (radioButton1.Checked)
            //{
            //    if (txtFre.Text.Equals(string.Empty))
            //    {
            //        MessageBox.Show("频率值不可为空！");
            //    }
            //    strFre = txtFre.Text;
            //}
            //else
            //{
            //    if (dtGridView.Rows.Count == 0)
            //    {
            //        MessageBox.Show("开始时间不可为空！");
            //    }
            //}
            this.Dispose();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows[0].Cells["StartTime"].Value == null)
            {
                return;
            }

            dtGridView.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
        }
    }
}
